//
//  listView.m
//  Mdongying
//
//  Created by mac pro -chenweishuai on 14-3-4.
//  Copyright (c) 2014年 伟帅陈. All rights reserved.
//

#import "listView.h"
#import "ListViewCell.h"
#import "AppUtility.h"

#define PID @"id"
#define TITLE @"name"
#define TableViewHeight 30

#define ListViewCellTag 3000

@implementation listView
@synthesize ListDelegate = _ListDelegate,selectedIndexPath,oneTableArray=_oneTableArray,twoTableArray=_twoTableArray;

-(void)dealloc
{
    [backGroundView release];
    [_one_tb release];
    [_two_tb release];
    [myScrollview release];
    [_oneTableArray release];
    [_twoTableArray release];
    [super dealloc];
}

-(void)requestData
{
    @autoreleasepool {
        
        NSString *postStr = [NSString stringWithFormat:@"id=0"];
        
        NSDictionary *resultDict =[NetUtility getDataWithURLStr:[NSString stringWithFormat:@"%@%@",KURLCityFromID,postStr] andPostStr:nil ];
        
        UEXNSLog(@"url = %@%@ ---- resultDic = %@",KURLCityFromID,postStr,resultDict);
        
        if (resultDict) {
            if ([resultDict objectForKey:@"d"]) {
                
                //                NSMutableDictionary *dic = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"热门城市",@"title",[[resultDict objectForKey:@"d"] objectForKey:@"hot"],@"list", nil];
                
                //                [mytableArray addObject:dic];
                //                [dic release];
                
                NSMutableDictionary * dddd = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@" ",@"id",@"全部",@"name" , nil];
                [_oneTableArray addObject:dddd];
                [_oneTableArray addObjectsFromArray:[resultDict objectForKey:@"d"]];
                
                
                
            }
        }
        [self performSelectorOnMainThread:@selector(refreshView) withObject:nil waitUntilDone:NO];
        
    }
    
    

}

-(void)refreshView
{
    myScrollview.contentSize = CGSizeMake(160,[_oneTableArray count]*TableViewHeight);
    [_one_tb setFrame:CGRectMake(0, 0, 160, [_oneTableArray count]*TableViewHeight)];
    [_one_tb reloadData];
}

- (id)initWithFrame:(CGRect)frame;
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];
        _oneTableArray = [[NSMutableArray alloc]init];
        _twoTableArray = [[NSMutableArray alloc]init];
        backGroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,[AppUtility screenWidth],300)];
        backGroundView.userInteractionEnabled = YES;
        backGroundView.backgroundColor = RGBCOLOR(250.0,245.0,241.0);
        [self addSubview:backGroundView];
        
        myScrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 160,backGroundView.frame.size.height)];
        myScrollview.backgroundColor = [UIColor clearColor];
        myScrollview.showsHorizontalScrollIndicator = NO;
        myScrollview.showsVerticalScrollIndicator = NO;
        [backGroundView addSubview:myScrollview];
    
        _one_tb = [[TableViewWithBlock alloc]initWithFrame:CGRectMake(0,0, 160,300) style:UITableViewStylePlain];
        _one_tb.backgroundColor = [UIColor clearColor];
        _one_tb.separatorStyle = NO;
        _one_tb.scrollEnabled = NO;
        [myScrollview addSubview:_one_tb];
        
        [NSThread detachNewThreadSelector:@selector(requestData) toTarget:self withObject:nil];

        
        [_one_tb initTableViewDataSourceAndDelegate:^(UITableView *tableView,int section){
            
            return (NSInteger)[_oneTableArray count];
            
        } setCellForIndexPathBlock:^(UITableView *tableView,NSIndexPath *indexPath){
            
            NSDictionary *dict =[_oneTableArray objectAtIndex:indexPath.row];
            static NSString *cellIdentifier = @"tableCell";
            ListViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

            if (!cell)
            {
           
                cell = [[[ListViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"tableCell"] autorelease];
                [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
                cell.backgroundColor = [UIColor clearColor];
                
                [cell setFrame:CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, TableViewHeight)];
                cell.nameLab.text = [dict objectForKey:TITLE];
                cell.jiantouImageV.hidden= NO;
               
            }
        
            return cell;
            
        } setDidSelectRowBlock:^(UITableView *tableView,NSIndexPath *indexPath){
            
            
            if (indexPath.row==0) {
                
                [self.ListDelegate  TouchCellwithListView:self WithDictory:[_oneTableArray objectAtIndex:0]];
                
                return ;

            }
    
            ListViewCell *selectedcell=(ListViewCell*)[tableView cellForRowAtIndexPath:self.selectedIndexPath];
            
            NSMutableDictionary *listDic = [_oneTableArray objectAtIndex:indexPath.row];
            NSString *postStr = [NSString stringWithFormat:@"id=%@",[listDic objectForKey:@"id"]];
            
            NSDictionary *resultDict =[NetUtility getDataWithURLStr:[NSString stringWithFormat:@"%@%@",KURLCityFromID,postStr] andPostStr:nil];
            UEXNSLog(@"KURL===%@%@ resultDict=%@",KURLCityFromID,postStr, resultDict);
            
            if (resultDict) {
                int stauts = [[resultDict objectForKey:@"s"] intValue];
                
                if (stauts==1) {

                    [_twoTableArray removeAllObjects];
                    UEXNSLog(@"twoArray = 1 = %@",_twoTableArray);
                    
                    
                    NSMutableDictionary *dic = [[NSMutableDictionary alloc]initWithObjectsAndKeys:[listDic objectForKey:@"id"],@"id",@"全部",TITLE,[listDic objectForKey:TITLE],@"ThreeTitle",nil];
                    [_twoTableArray addObject:dic];
                    [_twoTableArray addObjectsFromArray:[resultDict objectForKey:@"d"]];
                    
                    UEXNSLog(@"twoArray = 2 = %@",_twoTableArray);
                    [_two_tb reloadData];
                }
            }
            
            selectedcell.backView.backgroundColor = [UIColor whiteColor];
            selectedcell.leftImageV.backgroundColor = [UIColor clearColor];
            selectedcell.rightLightImageV.backgroundColor = RGBCOLOR(229.0,226.0,225.0);
            
            ListViewCell *cell=(ListViewCell*)[tableView cellForRowAtIndexPath:indexPath];
            cell.backView.backgroundColor = [UIColor clearColor];
            cell.leftImageV.backgroundColor = [UIColor redColor];
            cell.rightLightImageV.backgroundColor = [UIColor clearColor];
            
            NSIndexPath *index = indexPath;
            self.selectedIndexPath = index;

        }];
        
        
        _two_tb = [[TableViewWithBlock alloc]initWithFrame:CGRectMake(160,0, 160,backGroundView.frame.size.height) style:UITableViewStylePlain];
        _two_tb.backgroundColor = [UIColor clearColor];
        _two_tb.separatorStyle = NO;
        [backGroundView addSubview:_two_tb];
        
        [_two_tb initTableViewDataSourceAndDelegate:^(UITableView *tableView,int section){
            
            return (NSInteger)[_twoTableArray count];
            
        } setCellForIndexPathBlock:^(UITableView *tableView,NSIndexPath *indexPath){
            
            NSMutableDictionary *twoDic =[_twoTableArray objectAtIndex:indexPath.row];
            
            static NSString *cellIdentifier = @"tableCell";
            UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            if (!cell)
            {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"tableCell"] autorelease];
                cell.backgroundColor = [UIColor clearColor];
                [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
                [cell setFrame:CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, TableViewHeight)];
                
                UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0,0,160,TableViewHeight)];
                bgView.tag = ListViewCellTag+5;
                bgView.backgroundColor = [UIColor clearColor];
                [cell.contentView addSubview:bgView];
                [bgView release];
                
                //name
                UILabel *nameLab = [[UILabel alloc] initWithFrame:CGRectMake(25,10, 180, 17)];
                nameLab.backgroundColor = [UIColor clearColor];
                nameLab.font = [UIFont systemFontOfSize:12];
                nameLab.textColor = [UIColor blackColor];
                nameLab.tag = ListViewCellTag+11;
                [bgView addSubview:nameLab];
                [nameLab release];
                

            }
            
            UILabel *naLabe = (UILabel *)[cell.contentView viewWithTag:ListViewCellTag+11];
            naLabe.text =[twoDic objectForKey:TITLE];
            

            return cell;
            
        } setDidSelectRowBlock:^(UITableView *tableView,NSIndexPath *indexPath){
             
            NSMutableDictionary *dict =[_twoTableArray objectAtIndex:indexPath.row];
            
            if (indexPath.row==0) {
                
                
                NSMutableDictionary *dic11 = [[NSMutableDictionary alloc]initWithObjectsAndKeys:[dict objectForKey:@"id"],@"id",[dict objectForKey:@"ThreeTitle"],TITLE,nil];

                [self.ListDelegate  TouchCellwithListView:self WithDictory:dic11];
                

            }
            else
            {
                [self.ListDelegate  TouchCellwithListView:self WithDictory:dict];

            }
            
        }];
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
